﻿package jakey.SalaryCount;

import jakey.SalaryCount.SQLite.dbHelper;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class PersonInfoEditActivity extends Activity {
	private String user;
	private EditText nameEditText;
	private EditText salaryeEditText;
	private EditText majorEditText;
	private EditText infoEditText;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infoedit);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		dbHelper dbHelper = new dbHelper(this, "salaryCount_db", null);
		Intent intent = getIntent();
		user = intent.getStringExtra("user_name");
		Cursor cursor = dbHelper.select("user", null, "user_name=?", new String[]{user}, null, null, null);
		int salary = 0;
		String major = null, info = null;
		while(cursor.moveToNext()){
			salary = cursor.getInt(cursor.getColumnIndex("salary"));
			major = cursor.getString(cursor.getColumnIndex("major"));
			info = cursor.getString(cursor.getColumnIndex("info"));
		}
		cursor.close();
		dbHelper.close();
		nameEditText = (EditText)findViewById(R.id.editInfo_name);
		nameEditText.setText(user);
		
		salaryeEditText = (EditText)findViewById(R.id.editInfo_salary);
		salaryeEditText.setText(salary + "");
		
		majorEditText = (EditText)findViewById(R.id.editInfo_major);
		majorEditText.setText(major);
		
		infoEditText = (EditText)findViewById(R.id.editInfo_info);
		infoEditText.setText(info);
		
		((Button)findViewById(R.id.editInfo_updateBtn)).setOnClickListener(new UpdateBtnListener());
		((Button)findViewById(R.id.editInfo_clearBtn)).setOnClickListener(new ClearBtnListener());
	}
	
	class UpdateBtnListener implements OnClickListener {

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			dbHelper dbHelper = new dbHelper(PersonInfoEditActivity.this, "salaryCount_db", null);
			String nameString = nameEditText.getText().toString();
			String majorString = majorEditText.getText().toString();
			String infoString = infoEditText.getText().toString();
			int salary = Integer.parseInt(salaryeEditText.getText().toString());
			dbHelper.exec("UPDATE user SET user_name= '" + nameString + "', salary='" + salary + "',major='" + majorString + "',info='"+ infoString + "' WHERE user_name='" + user + "'");
			dbHelper.close();
			
			PersonInfoEditActivity.this.finish();
		}		
	}
	
	class ClearBtnListener implements OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			nameEditText.setText("");
			salaryeEditText.setText("");
			majorEditText.setText("");
			infoEditText.setText("");
		}	
	}
	
}
